package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.item.TbItemCat;
import com.dongdongshop.pojo.typeTemplate.TbTypeTemplate;
import com.dongdongshop.service.ItemCatServiceI;
import com.dongdongshop.service.TemplateServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

/**
 *分类管理
 */
@Controller
@RequestMapping("itemCat")
public class ItemCatController {
    private final Logger logger = LoggerFactory.getLogger(ItemCatController.class);

    @Reference
    private ItemCatServiceI itemCatServiceI;
    @Reference
    private TemplateServiceI templateServiceI;

    @RequestMapping("showItemCat")
    public String showItemCat(Model model){
        List<TbTypeTemplate> templateList=templateServiceI.findAll();
        model.addAttribute("templates",templateList);
        return "admin/item_cat";
    }

    @RequestMapping("ItemCatList")
    @ResponseBody
    public Result<List<TbItemCat>> ItemCatList(@RequestParam(defaultValue = "0")Long parentId){
        try {
            List<TbItemCat> itemCatList=itemCatServiceI.itemCatList(parentId);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(itemCatList);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("getItemById/{id}")
    @ResponseBody
    public Result<TbItemCat> getItemById(@PathVariable("id")Long id){
        try {
            if (id==0){
                return Result.jsonResponse(HttpResponseEnum.OK).setData("顶级分类列表");
            }else {
                TbItemCat tbItemCat=itemCatServiceI.getItemById(id);
                return Result.jsonResponse(HttpResponseEnum.OK).setData(tbItemCat);
            }
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("deleteItemCat")
    @ResponseBody
    public Result deleteItemCat(@RequestParam("ids[]")Integer[] ids){
        try {
            itemCatServiceI.deleteItemCat(ids);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("insertItemCat")
    @ResponseBody
    public Result insertItemCat(TbItemCat tbItemCat){
        System.out.println("ddd"+tbItemCat);
        try {
            itemCatServiceI.insertItemCat(tbItemCat);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("getItemCat/{id}")
    @ResponseBody
    public Result<TbItemCat> getItemCat(@PathVariable("id")Long id){
        try {
            TbItemCat itemCat=itemCatServiceI.getItemCat(id);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(itemCat);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("updateItemCat")
    @ResponseBody
    public Result updateItemCat(TbItemCat tbItemCat){
        try {
            itemCatServiceI.updateItemCat(tbItemCat);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
}
